home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib25 / bcmp.c < prev    next >
C/C++ Source or Header  |  1992-10-15  |  1KB  |  69 lines

  1. #include <stddef.h>
  2. #include <string.h>
  3. #include <assert.h>
  4.  
  5. #undef ODD
  6. #define ODD(x) ((int)((long)(x) & 1L))
  7.  
  8. /*
  9.  * compare n bytes efficientlty
  10.  *
  11.  * odd/even handled correctly
  12.  *
  13.  *   ++jrb  bammi@dsrgsun.ces.cwru.edu
  14.  */
  15.  
  16. #define INC(x, size) x = (const void *)(((const char *)(x)) + (size))
  17.  
  18. int bcmp(src, dst, n)
  19. const void * src;
  20. const void * dst;
  21. register size_t n;
  22. {
  23.  
  24.     assert ((src != NULL) && (dst != NULL));
  25.  
  26.     if((src != dst) && (n > 0))
  27.     {
  28.     register size_t l, w;
  29.     
  30.     switch(ODD(src) + ODD(dst))
  31.     {
  32.       case 2: /* ODD ODD */
  33.         if (*(const char *)dst != *(const char *)src)
  34.         return 1;
  35.             INC(src, 1);
  36.         INC(dst, 1);
  37.         n--;
  38.         /* fall thru */
  39.         
  40.       case 0: /* EVEN EVEN */
  41.         l = (n >> 2); /* # of longs */
  42.         n -= (l << 2);
  43.         w = (n >> 1); /* # of words */
  44.         n -= (w << 1); /* n == # of residual bytes */
  45.         while(l--) {
  46.         if (*((const long *)dst) != *((const long *)src))
  47.             return 1;
  48.         INC(dst, sizeof(long));
  49.         INC(src, sizeof(long));
  50.         }
  51.         while(w--) {
  52.         if( *((const short *)dst) != *((const short *)src) )
  53.             return 1;
  54.         INC(dst, sizeof(short));
  55.         INC(src, sizeof(short));
  56.         }
  57.         /* fall thru */
  58.       case 1: /* ODD/EVEN or EVEN/ODD */
  59.         while(n--) {
  60.         if( *(const char *)dst != *(const char *)src )
  61.             return 1;
  62.         INC(dst, 1);
  63.         INC(src, 1);
  64.         }
  65.     }
  66.     }
  67.      return 0;
  68. }
  69.